//
// Created by D00371 on 2021/10/6.
//

#include "host_bind_filter.h"
#include "easylogging++.h"
#include "ContextBindHandler.h"

INITIALIZE_EASYLOGGINGPP

class EasyLoggingDefaultConfig {
public:
    EasyLoggingDefaultConfig() {
        try {
            el::Configurations defaultConf;
            defaultConf.setToDefault();
            // Values are always std::string
            defaultConf.set(el::Level::Global,el::ConfigurationType::Format, "%datetime %levshort %msg");
            defaultConf.set(el::Level::Global, el::ConfigurationType::Filename, "D:\\svn\\svn.log");
            defaultConf.set(el::Level::Global, el::ConfigurationType::ToFile, "true");
            defaultConf.set(el::Level::Global, el::ConfigurationType::MaxLogFileSize, "20971520");
#ifndef _DEBUG
            defaultConf.set(el::Level::Global, el::ConfigurationType::ToStandardOutput, "false");
            defaultConf.set(el::Level::Debug, el::ConfigurationType::Enabled, "false");
//            defaultConf.set(el::Level::Global, el::ConfigurationType::Enabled, "false");
//            defaultConf.set(el::Level::Global, el::ConfigurationType::ToFile, "false");
#endif
            // default logger uses default configurations
            el::Loggers::reconfigureAllLoggers(defaultConf);
        } catch (const std::exception& e) {

        }
    }
};
static EasyLoggingDefaultConfig config;

int user_host_filter(const char* auth, char* host){
    if(ContextBindHandler::GetInstance()->Check(auth, host)) {
        return 0;
    }
    return -1;
}

void load_context_host(const char* filename) {
    ContextBindHandler::GetInstance()->Load(filename);
}

void show_request(const char* fmt, ...) {
    va_list arg;
    va_start( arg, fmt);
    char buffer[1024] = {0};
    vsprintf_s(buffer, sizeof(buffer), fmt, arg);
    LOG(INFO)<<"REQ: "<< buffer;
    va_end(arg);
}